function DefaultValue (obj, is_integer) {
	if ((is_integer && isNaN(obj.value)) || obj.value == "") {		
		obj.value = obj.defaultValue;
		obj.select();			
	} else {
		// pass validate, now set the default value as new value		
		obj.defaultValue = obj.value;
	}		
}

function TextFieldSizeLimit (obj, limit) {
	if (obj.value.length > limit) return false;
	
	return true;
}

// not allow to edit
function NoEdit (obj) {
	obj.blur();	
}

function Round (num, round_num) {
	if (num == "" || round_num =="") return 0;
	if (isNaN (num) || isNaN(round_num)) return 0; 
	return Math.round(parseFloat(num) * Math.pow(10, round_num))/Math.pow(10, round_num);		
}

function FormatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    
    if(isNaN(num))
        num = "0";
        
    var sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    var cents = num%100;
    num = Math.floor(num/100).toString();
    
    if(cents<10)
        cents = "0" + cents;
    
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3)) + num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + '.' + cents);
}

function AddThousandSeparators( InputNumber )
{    
    var x = 0;
    var IsNegative = false;
    
    if (InputNumber < 0) {
        x = 0 - InputNumber;
        IsNegative = true;
    } else {
        x = InputNumber;
    }
   
    x+="";

    iLen=x.length;
    pos=x.indexOf(".");
    
    if (pos>-1) //there are decimals
    {
        iLen=pos;
    }

    temp="";

    temp=x.substring(iLen,x.length);
    
    for (var i=iLen-1;i>=0;i--) {

        if ((iLen-i-1)%3==0&&i!=iLen-1)
            temp=x.charAt(i)+","+temp;
        else
            temp=x.charAt(i)+temp;
    }
    
    
    return (IsNegative)?( 0 - temp):temp;
}

function ArrayLookup (array, str) {
	
	for (var x = 0; x < array.length; x++) {
		if (array[x] == str) return true;
	}
	
	return false;
}

function DateDiff (interval, start, end){
 
  switch (interval) {
    case "s":return parseInt((start - end) / 1000);
    case "n":return parseInt((start - end) / 60000);
    case "h":return parseInt((start - end) / 3600000);
    case "d":return parseInt((start - end) / 86400000);
    case "w":return parseInt((start - end) / (86400000 * 7));
    case "m":return (start.getMonth()+1)+((start.getFullYear()-end.getFullYear())*12) - (end.getMonth()+1);
    case "y":return start.getFullYear() - end.getFullYear();
  }
}

function DateConvert(date_str, format_using, delim) {

	if (date_str != "") {
		var date_array = date_str.split(delim);
						
		if (date_array.length == 3) {
			switch (format_using) {
				case "MMDDYYYY":
					return (new Date(date_array[2], date_array[0]-1, date_array[1]));
					break;
						
				case "DDMMYYYY":
					return (new Date(date_array[2], date_array[1]-1, date_array[0]));
					break;
						
				case "YYYYDDMM":
					return (new Date(date_array[0], date_array[2]-1, date_array[1]));
					break;
						
				case "YYYYMMDD":
					return (new Date(date_array[0], date_array[1]-1, date_array[2]));
					break;			
			}
		}
	}
		
	return "";	// return nothing
}

function OpenWindow (url, height, width) {

	if (window.showModalDialog) {
		window.showModalDialog (url, window, "center:yes;resizable:no;status:no;dialogHeight:" + height + "px;dialogWidth:" + width + "px;");
	} else {
		window.open(url, "", "scrollbars=yes,width=" + width + ",height=" + height);
	}				
	

}

function RequestQueryString (name) {
	var result = "";
	var query_str = document.location.search;
	
	if (query_str != "") {
		query_str = query_str.substr(1);	// get rid of "?"
		
		var query_array = query_str.split("&");
		for (var x = 0; x < query_array.length; x++) {
					
			if (query_array[x].indexOf (name + "=") == 0) {

				result = query_array[x].substr ((name.length + 1));
				break;
			}			
		}
	}
	
	return result;
}

function DateCompare (start, end) {
	var d_start = DateConvert (start, DateFormat, "/");
	var d_end = DateConvert (end, DateFormat, "/");
	
	if (d_start > d_end)
		return false;
	
	return true;
}

function SetDefault (obj, field) {
	if (obj.selectedIndex > 0) {
		// check for special gl
		var matched = false;
		
		for (var x = 0; x < special_glcode.length; x++) {
			if (obj[obj.selectedIndex].text.indexOf(special_glcode[x]) >= 0) {
				matched = true;
				break;
			}
		}		
		
		field.value = (matched)?"":obj[obj.selectedIndex].text;	
	}
}

function CancelButton () {
	window.close();
}

function DivertEnter (field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;	
	alert (keyCode);
	return ((keyCode == 13)?false:true);

}

function FormatNumberBy3(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}

function MaxLengthCheck (obj, MaxLength) {
	
	return ((obj.value.length == MaxLength)?false:true);
	


}
